﻿using AlarmClock.BLL;
using Model;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace AlarmClock.UI
{
    public partial class FrmClockSetting : Form
    {
        private Timer timerForTakeMousePos;
        private ClockBLL clockBLL = new ClockBLL();
        private ClockConfig clockConfig;

        public FrmClockSetting()
        {
            InitializeComponent();

            this.ShowInTaskbar = false;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            //窗体边框阴影效果
            PPWin32.SetClassLong(this.Handle, PPWin32.GCL_STYLE, PPWin32.GetClassLong(this.Handle, PPWin32.GCL_STYLE) | PPWin32.CS_DropSHADOW);

            //初始化界面配置
            clockConfig = clockBLL.ReadAllClockConfig();

            check_ShowWeather.Checked = clockConfig.alarmingShowWeather;
            tip_ShowWeather.Text = clockConfig.alarmingShowWeather ? "显示本地天气" : "不显示";

            check_appAutoStart.Checked = clockConfig.appAutoStart;
            tip_appAutoStart.Text = clockConfig.appAutoStart ? "开启" : "关闭";

            check_isExitToMiniMode.Checked = clockConfig.isExitToMiniMode;
            tip_isExitToMiniMode.Text = clockConfig.isExitToMiniMode ? "最小化到系统托盘" : "退出程序";

            check_createDeskShortCut.Checked = clockConfig.createDeskShortCut;
            tip_createDeskShortCut.Text = clockConfig.createDeskShortCut ? "已创建" : "未创建（如更新，可重新生成）";
        }

        public FrmClockSetting(Form frmClock) : this()
        {
            //添加鼠标悬浮panel效果
            foreach (Control c in flowLayoutPanel.Controls)
            {
                Service.SetControlsStyle(c, c);
            }

            this.Load += FrmClockSetting_Load;
            this.FormClosing += FrmClockSetting_Closing;

            this.Height = frmClock.Height;//出现在主窗体右侧，并等高显示
            Point p = new Point(frmClock.Location.X + frmClock.Width, frmClock.Location.Y);
            Size s = Screen.PrimaryScreen.Bounds.Size;
            p.X = p.X > s.Width - this.Width ? s.Width - this.Width : p.X;
            this.Location = p;//左右边的间距不够，就取屏幕与控件的宽度差

            timerForTakeMousePos = new Timer();
            timerForTakeMousePos.Interval = 50;//0.05秒
            timerForTakeMousePos.Tick += new EventHandler(time_Tick);
            timerForTakeMousePos.Start();

            //设置关闭时程序最小化
            check_isExitToMiniMode.Click += (sender, e) => {
                clockConfig.isExitToMiniMode = check_isExitToMiniMode.Checked;
                tip_isExitToMiniMode.Text = clockConfig.isExitToMiniMode ? "最小化到系统托盘" : "退出程序";

                clockBLL.UpdateClockConfig(clockConfig);
            };

            //设置闹钟时显示本地天气
            check_ShowWeather.Click += (sender, e) => {
                clockConfig.alarmingShowWeather = check_ShowWeather.Checked;
                tip_ShowWeather.Text = clockConfig.alarmingShowWeather ? "显示本地天气" : "不显示";

                clockBLL.UpdateClockConfig(clockConfig);
            };

            //设置开机自启动
            check_appAutoStart.Click += (sender, e) =>
            {
                clockConfig.appAutoStart = check_appAutoStart.Checked;
                tip_appAutoStart.Text = clockConfig.appAutoStart ? "开启" : "关闭";

                string sourseEXE = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AlarmClock.UI.exe");
                string startup_ShortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\闹钟.lnk";
                if (clockConfig.appAutoStart)//需要创建开机自启动目录快捷键
                {
                    if (File.Exists(startup_ShortcutPath))
                    {
                        File.Delete(startup_ShortcutPath);
                    }
                    _ = Service.CreateShortcut(sourseEXE, startup_ShortcutPath);
                }
                else
                {
                    if (File.Exists(startup_ShortcutPath))
                    {
                        File.Delete(startup_ShortcutPath);
                    }
                }
                clockBLL.UpdateClockConfig(clockConfig);
            };

            //设置闹钟创建桌面快捷方式
            check_createDeskShortCut.Click += (sender, e) =>
            {
                clockConfig.createDeskShortCut = check_createDeskShortCut.Checked;
                tip_createDeskShortCut.Text = clockConfig.createDeskShortCut ? "已创建" : "未创建（如更新，可重新生成）";

                string Desktop_ShortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\闹钟.lnk";
                string sourseEXE = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AlarmClock.UI.exe");

                if (clockConfig.createDeskShortCut)//需要创建桌面快捷键
                {
                    if (File.Exists(Desktop_ShortcutPath))
                    {
                        File.Delete(Desktop_ShortcutPath);
                    }
                    _ = Service.CreateShortcut(sourseEXE, Desktop_ShortcutPath);
                }
                else
                {
                    if (File.Exists(Desktop_ShortcutPath))
                    {
                        File.Delete(Desktop_ShortcutPath);
                    }
                }
                clockBLL.UpdateClockConfig(clockConfig);
            };
        }

        private void time_Tick(object sender, EventArgs e)
        {
            Point p = Control.MousePosition;
            MouseButtons click = Control.MouseButtons;

            bool IsInSettingArea = this.Bounds.Contains(p.X, p.Y);

            if (!IsInSettingArea && (click == MouseButtons.Left || click == MouseButtons.Right || click == MouseButtons.Middle))
            {
                this.timerForTakeMousePos.Stop();
                this.Close();
                this.Dispose();
            }
        }

        #region 窗体渐入渐出效果

        private void FrmClockSetting_Load(object sender, EventArgs e)
        {
            PPWin32.AnimateWindow(this.Handle, 200, PPWin32.AW_SLIDE | PPWin32.AW_HOR_POSITIVE);
        }

        private void FrmClockSetting_Closing(object sender, FormClosingEventArgs e)
        {
            PPWin32.AnimateWindow(this.Handle, 200, PPWin32.AW_SLIDE | PPWin32.AW_HIDE);
        }

        #endregion

        /// <summary>
        /// 不占用窗口焦点
        /// </summary>
        protected override bool ShowWithoutActivation => true;
    }
}
